package org.lyfeng.spring.beans.definitions;

import java.util.LinkedHashSet;
import java.util.Set;

import org.lyfeng.spring.exception.BeanDefinitionExcetion;
import org.lyfeng.spring.utils.ClassUtils;

/**
 * bean的相关属性类，包括bean名，关联的类名(包含包名)
 * 依赖的bean，该类没有做同步处理
 * @author leiyufeng
 * 2013-4-23 上午10:43:12
 */
public class AbstractBeanDefinition implements BeanDefinition {
	protected String beanName; //bean名
	protected String className; //类名
	protected String wholeClassName;//包括包名
	protected Class<?> resolvedClass; //class对象
	protected Set<PropertyValue> properties=new LinkedHashSet<PropertyValue>(); //依赖的属性信息 name-->ref
	@SuppressWarnings("unchecked")
	@Override
	public Class<?> getResolvedClass() {
		return resolvedClass;
	}

	public void setResolvedClass(Class<?> resolvedClass) {
		this.resolvedClass = resolvedClass;
	}

	public void setProperties(Set<PropertyValue> properties) {
		this.properties = properties;
	}

	public String getWholeClassName() {
		return wholeClassName;
	}

	public void setWholeClassName(String wholeClassName) {
		this.wholeClassName = wholeClassName;
	}

	@Override
	public String getBeanName() {
		return this.beanName;
	}

	@Override
	public void setBeanName(String beanName) {
		this.beanName=beanName;
	}

	@Override
	public String getClassName() {
		return this.className;
	}

	@Override
	public void setClassName(String className) {
		this.className=className;
	}
	
	@Override
	public Set<String> getProperties(String propertyName) {
		if(null==propertyName)
			throw new BeanDefinitionExcetion("传入propertyName不能为空");
		Set<String> deptBeans=new LinkedHashSet<String>();
		for(PropertyValue p:properties)
		{
			if(propertyName.trim().equals(p.getDeptName()))
			{
				deptBeans.add(p.getRefBean());
			}
		}
		return deptBeans;
	}

	@Override
	public void addProperty(String propertyName, String refName) {
		PropertyValue property=new PropertyValue(propertyName, refName);
		properties.add(property);
	}

	@Override
	public Set<PropertyValue> getProperties() {
		return this.properties;
	}
	
	public Class<?> resolveBeanClass(ClassLoader classLoader)
	{
		String className=this.wholeClassName;
		if(null==className)
		{
			return null;
		}
		Class<?> resolvedClass=ClassUtils.forName(className, classLoader);
		this.resolvedClass=resolvedClass;
		return resolvedClass;
	}
}
